const query = require("../db/query");

//  获取药品 包含模糊搜索
const Getmedicinelist = async (params) => {
    let sql = `select * from restoratives where restorativesname LIKE '%${params.restorativesname}%' ORDER BY restorativesid desc`;
    return await query(sql)
}

// 获取药品的大类型
const Getmedicinetype = async () => {
    let sql = `select * from drugtype`;
    return await query(sql)
}

// 获取药品大类型下的小类型
const Getmedicinetypes = async (params) => {
    let sql = `SELECT *  FROM drug_types WHERE Drugtypespid = (SELECT Drugtypeid FROM drugtype WHERE Drugtypetitle = '${params.Drugtypetitle}')`
    return await query(sql)
}

// 添加药品
const Addmedicinelist = async (params) => {
    let sql = `INSERT INTO restoratives (restorativesname,restorativesprice,restorativescount,restorativesinven,restorativesstate,restorativestitle,restorativesimage,restorativesdesc,restorativestype) VALUES ('${params.restorativesname}','${params.restorativesprice}','1', '${params.restorativesinven}', '${params.restorativesstate}','${params.restorativestitle}','${params.restorativesimage}','${params.restorativesdesc}','${params.restorativestype}');`
    return await query(sql)
}

// 删除或者说是下架药品
const Delmedicinelist = async (params) => {
    let sql = `DELETE FROM restoratives WHERE restorativesid = ${params.restorativesid}`
    return await query(sql)
}

// 修改药品的数据
const Updateinventory = async (params) => {
    let sql = `UPDATE restoratives SET restorativesname ='${params.restorativesname}', restorativesprice='${params.restorativesprice}',restorativesinven='${params.restorativesinven}',restorativesstate = '${params.restorativesstate}', restorativestitle='${params.restorativestitle}',restorativesimage='${params.restorativesimage}',restorativesdesc='${params.restorativesdesc}',restorativestype='${params.restorativestype}' WHERE restorativesid = ${params.restorativesid}`
    return await query(sql)
}

module.exports = {
    Getmedicinelist, //获取药品包含模糊搜索
    Getmedicinetype, //获取药品大类型
    Getmedicinetypes, //获取药品大类型下的小类型
    Addmedicinelist, //添加药品
    Delmedicinelist,//删除或者说是下架药品
    Updateinventory, //修改药品的数据
}